﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using PointOfSale.Global;
using PointOfSale.Class;

namespace PointOfSale.Configuration.User
{
	public class frmPOSUsers : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		public static System.Windows.Forms.ListView lsvPOSUser;
		private System.Windows.Forms.ColumnHeader columUserName;
		private System.Windows.Forms.ColumnHeader columUserFullName;
		private System.Windows.Forms.ColumnHeader columnDepartment;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.DialogResult ans;
		private MyXPButton.MyXPButton btnAdd;
		private MyXPButton.MyXPButton btnDelete;
		private MyXPButton.MyXPButton btnEdit;
		private MyXPButton.MyXPButton btnOK;
		private System.Windows.Forms.ColumnHeader columnUserId;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnDepartmentId;
		private System.Windows.Forms.ColumnHeader columnPrivilige;
		
		private System.ComponentModel.Container components = null;

		public frmPOSUsers()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.btnEdit = new MyXPButton.MyXPButton();
			this.btnDelete = new MyXPButton.MyXPButton();
			this.btnAdd = new MyXPButton.MyXPButton();
			lsvPOSUser = new System.Windows.Forms.ListView();
			this.columUserName = new System.Windows.Forms.ColumnHeader();
			this.columUserFullName = new System.Windows.Forms.ColumnHeader();
			this.columnDepartment = new System.Windows.Forms.ColumnHeader();
			this.columnUserId = new System.Windows.Forms.ColumnHeader();
			this.columnDepartmentId = new System.Windows.Forms.ColumnHeader();
			this.columnPrivilige = new System.Windows.Forms.ColumnHeader();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.btnOK = new MyXPButton.MyXPButton();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.btnEdit);
			this.groupBox1.Controls.Add(this.btnDelete);
			this.groupBox1.Controls.Add(this.btnAdd);
			this.groupBox1.Controls.Add(lsvPOSUser);
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(376, 216);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "POS Users Operation";
			// 
			// btnEdit
			// 
			this.btnEdit.AdjustImageLocation = new System.Drawing.Point(0, 0);
			this.btnEdit.BtnShape = MyXPButton.emunType.BtnShape.Rectangle;
			this.btnEdit.BtnStyle = MyXPButton.emunType.XPStyle.Default;
			this.btnEdit.Location = new System.Drawing.Point(208, 184);
			this.btnEdit.Name = "btnEdit";
			this.btnEdit.TabIndex = 6;
			this.btnEdit.Text = "Edit";
			this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
			// 
			// btnDelete
			// 
			this.btnDelete.AdjustImageLocation = new System.Drawing.Point(0, 0);
			this.btnDelete.BtnShape = MyXPButton.emunType.BtnShape.Rectangle;
			this.btnDelete.BtnStyle = MyXPButton.emunType.XPStyle.Default;
			this.btnDelete.Location = new System.Drawing.Point(296, 184);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.TabIndex = 5;
			this.btnDelete.Text = "Delete";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// btnAdd
			// 
			this.btnAdd.AdjustImageLocation = new System.Drawing.Point(0, 0);
			this.btnAdd.BtnShape = MyXPButton.emunType.BtnShape.Rectangle;
			this.btnAdd.BtnStyle = MyXPButton.emunType.XPStyle.Default;
			this.btnAdd.Location = new System.Drawing.Point(120, 184);
			this.btnAdd.Name = "btnAdd";
			this.btnAdd.TabIndex = 4;
			this.btnAdd.Text = "Add";
			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
			// 
			// lsvPOSUser
			// 
			lsvPOSUser.CheckBoxes = true;
			lsvPOSUser.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						 this.columUserName,
																						 this.columUserFullName,
																						 this.columnDepartment,
																						 this.columnUserId,
																						 this.columnDepartmentId,
																						 this.columnPrivilige});
			lsvPOSUser.FullRowSelect = true;
			lsvPOSUser.GridLines = true;
			lsvPOSUser.ImeMode = System.Windows.Forms.ImeMode.On;
			lsvPOSUser.Location = new System.Drawing.Point(16, 24);
			lsvPOSUser.Name = "lsvPOSUser";
			lsvPOSUser.Size = new System.Drawing.Size(352, 152);
			lsvPOSUser.TabIndex = 3;
			lsvPOSUser.View = System.Windows.Forms.View.Details;
			// 
			// columUserName
			// 
			this.columUserName.Text = "User Name";
			this.columUserName.Width = 100;
			// 
			// columUserFullName
			// 
			this.columUserFullName.Text = "User Full Name";
			this.columUserFullName.Width = 148;
			// 
			// columnDepartment
			// 
			this.columnDepartment.Text = "Department";
			this.columnDepartment.Width = 100;
			// 
			// columnUserId
			// 
			this.columnUserId.Width = 0;
			// 
			// columnDepartmentId
			// 
			this.columnDepartmentId.Width = 0;
			//
			// columnPrivilige
			//
			this.columnPrivilige.Width = 0;
			// 
			// columnHeader1
			// 
			this.columnHeader1.Width = 0;
			// 
			// btnOK
			// 
			this.btnOK.AdjustImageLocation = new System.Drawing.Point(0, 0);
			this.btnOK.BtnShape = MyXPButton.emunType.BtnShape.Rectangle;
			this.btnOK.BtnStyle = MyXPButton.emunType.XPStyle.Default;
			this.btnOK.Location = new System.Drawing.Point(304, 232);
			this.btnOK.Name = "btnOK";
			this.btnOK.TabIndex = 1;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// frmPOSUsers
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(390, 260);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmPOSUsers";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "POS Users";
			this.Load += new System.EventHandler(this.frmPOSUsers_Load);
			this.Activated += new System.EventHandler(this.frmPOSUsers_Activated);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

			this.groupBox1.Text = clsGlobal.g_Hashtable["UserOperation"].ToString();
			this.btnEdit.Text = clsGlobal.g_Hashtable["UserFormbtnEdit"].ToString();
			this.btnDelete.Text = clsGlobal.g_Hashtable["UserFormbtnDelete"].ToString();
			this.btnAdd.Text = clsGlobal.g_Hashtable["UserFormbtnAdd"].ToString();
			this.columUserName.Text = clsGlobal.g_Hashtable["UserFormUserName"].ToString();
			this.columUserFullName.Text = clsGlobal.g_Hashtable["UserFormUserFullName"].ToString();
			this.columnDepartment.Text = clsGlobal.g_Hashtable["UserFormDepartment"].ToString();
			this.btnOK.Text = clsGlobal.g_Hashtable["UserFormbtnOK"].ToString();
			this.Text = clsGlobal.g_Hashtable["UserFormName"].ToString();
		}
		#endregion

		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			frmAddPOSUser _frmAddPOSUser = new frmAddPOSUser();
			_frmAddPOSUser.TypeUse = 1;
			_frmAddPOSUser.ShowDialog();
		}

		private void btnEdit_Click(object sender, System.EventArgs e)
		{
			frmAddPOSUser _frmAddPOSUser = new frmAddPOSUser();
			int i = lsvPOSUser.SelectedItems.Count;
			if( i == 0)
			{
				MessageBox.Show("Choose an User to edit!");
			
			}
			else
			{
				_frmAddPOSUser.TypeUse = 2;
				_frmAddPOSUser.UserName = lsvPOSUser.SelectedItems[0].SubItems[0].Text.ToString();
				_frmAddPOSUser.UserFullName = lsvPOSUser.SelectedItems[0].SubItems[1].Text.ToString();
				_frmAddPOSUser.DepartmentId = System.Convert.ToInt32(lsvPOSUser.SelectedItems[0].SubItems[4].Text.ToString());
				_frmAddPOSUser.UserId = System.Convert.ToInt32(lsvPOSUser.SelectedItems[0].SubItems[3].Text.ToString());
				_frmAddPOSUser.Privilige = System.Convert.ToBoolean(lsvPOSUser.SelectedItems[0].SubItems[5].Text.ToString());
				_frmAddPOSUser.ShowDialog();
				
			}
			
		}

		private void frmPOSUsers_Load(object sender, System.EventArgs e)
		{
			btnDelete.Enabled = true;
			LoadUserToListView();
		}
        
		public delegate void Call();
		public static void LoadUserToListView()
		{
			lsvPOSUser.Items.Clear();
			try
			{
				SqlDataReader dre = clsUser.GetAllDataToDataReader();
				string strUserName = "";
				string strUserFullName = "";
				string strDeptName = "";
				string strUserId = "";
				string strDepartmentId = "";
				string strPrivilige = "";
				while (dre.Read())
				{
					strUserName= dre["UserName"].ToString();
					strUserFullName = dre["UserFullName"].ToString();
					strDeptName = dre["DepartmentName"].ToString();
					strUserId = dre["UserId"].ToString();
					strDepartmentId = dre["DepartmentId"].ToString();
					strPrivilige = dre["Privilige"].ToString();
					ListViewItem listViewItemk = new ListViewItem(new string[] {strUserName, strUserFullName, strDeptName, strUserId, strDepartmentId, strPrivilige});
					lsvPOSUser.Items.Add(listViewItemk);
				}	
				dre.Close ();
			}
			catch(Exception ex)
			{
				MessageBox.Show (ex.ToString());			
			}
			
		}

		private void frmPOSUsers_Activated(object sender, System.EventArgs e)
		{
			//LoadUserToListView();
		}
		private void DeleteUser(int intUserId)
		{
			clsUser.Delete(intUserId);
		}
		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			int intUserIdDel = 0;
			System.Windows.Forms.ListViewItem[] MyArrayListViewItem = new ListViewItem[1000];
			int i = lsvPOSUser .CheckedItems .Count ;
			int j = 0;
			if(i==0)
			{
				MessageBox.Show ("Chọn người dùng để xóa!","Chú ý!",MessageBoxButtons.OK ,MessageBoxIcon.Error ); 
			}
			else
			{
				for(j=0;j<i; j++)
				{
					MyArrayListViewItem[j] = lsvPOSUser.CheckedItems[j];
				}
				ans = MessageBox.Show("Bạn có muốn xóa không?","Xác nhận",MessageBoxButtons.YesNo , MessageBoxIcon.Question);
				if(  ans == DialogResult.Yes )
				{
					for( j = 0;j < i ;j++)
					{
						intUserIdDel = System.Convert.ToInt32(MyArrayListViewItem[j].SubItems[3].Text.Trim().ToString());
						this.DeleteUser(intUserIdDel);
					}
									
				}
				else
				{
					return;			
				}
				LoadUserToListView();
			}
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}
